package com.liu.nb.security;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.liu.nb.util.Const;
import com.liu.nb.util.RedisUtil;
import com.liu.nb.util.exception.CaptchaException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

/*
@name:
@description:判断验证码是否正确的过滤器
            继承OncePerRequestFilter表示这个过滤器只执行一次
@author: lzy
@title: 天王盖地虎!!!
@time:  2021-08-07 12:40:30
*/
@Component
public class CaptchaFilter extends OncePerRequestFilter {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private LoginFailureHandler loginFailureHandler;
    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        String url=httpServletRequest.getRequestURI();
        //如果是login并且是post请求
        if ("/login".equals(url)&&httpServletRequest.getMethod().equals("POST")){
        //校验验证码
            try {
                validate(httpServletRequest);
            } catch (CaptchaException e) {
                //如果验证码不满足条件就调用登录失败的处理器
                loginFailureHandler.onAuthenticationFailure(httpServletRequest,httpServletResponse,e);
            }
            //如果不正确，就跳转到认证失败处理器
        }
        filterChain.doFilter(httpServletRequest,httpServletResponse);
    }

    //校验验证码逻辑
    private void validate(HttpServletRequest httpServletRequest){
//        HashMap<String,String> hashMap = requestParm(httpServletRequest);
        String code=httpServletRequest.getParameter("captcha");
        String key=httpServletRequest.getParameter("token");
        //判断是否为空
        if (StringUtils.isBlank(code)&&StringUtils.isBlank(key)){
            throw new CaptchaException("验证码错误！");
        }
        //和redis中的验证码进行比较
        if (!code.equals(redisUtil.hget(Const.CAPTCHA_KEY,key))){
            throw new CaptchaException("验证码错误！");
        }
        redisUtil.hdel(Const.CAPTCHA_KEY,key);
    }
    //把前端传过来的json数据转换成map(在前端把数据用qs序列化一下后端就不用这样做了)
   /* private HashMap<String,String> requestParm(HttpServletRequest httpServletRequest){
        StringBuffer sb=new StringBuffer();
        char[] chars=new char[1024];
        try {
            InputStreamReader inputStreamReader=new InputStreamReader(httpServletRequest.getInputStream());
            while (inputStreamReader.read(chars)!=-1){
                sb.append(chars);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JSON.parseObject(sb.toString(),HashMap.class);
    }*/
}
